package dice.sinanya.dice.system;

import dice.sinanya.db.properties.ban.SelectBanProperties;
import dice.sinanya.db.properties.game.SelectGameProperties;
import dice.sinanya.db.properties.system.SelectSystemProperties;
import dice.sinanya.db.system.GetPropertiesSwitch;
import dice.sinanya.db.system.SetPropertiesSwitch;
import dice.sinanya.entity.EntityTypeMessages;
import dice.sinanya.exceptions.NotProipertiesSwitch;

import java.util.ArrayList;

import static dice.sinanya.system.MessagesSystem.SPACE;
import static dice.sinanya.system.MessagesTag.TAG_PROPERTIES;
import static dice.sinanya.tools.makedata.Sender.sender;
import static dice.sinanya.tools.makedata.StringFormatById.stringFormatById;

public class Properties {
    private final EntityTypeMessages entityTypeMessages;

    public Properties(EntityTypeMessages entityTypeMessages) {
        this.entityTypeMessages = entityTypeMessages;
    }

    public void prop() throws NotProipertiesSwitch {
        String tag = TAG_PROPERTIES;
        String msg = entityTypeMessages.getMsgGet().getMsg().trim().replaceFirst(tag.substring(0, tag.length() - 2), "").trim();
        GetPropertiesSwitch getPropertiesSwitch = new GetPropertiesSwitch();
        ArrayList<String> propSystem = getPropertiesSwitch.getSystemColumn();
        ArrayList<String> banSystem = getPropertiesSwitch.getBanColumn();
        ArrayList<String> gameSystem = getPropertiesSwitch.getGameColumn();
        if (msg.contains(SPACE) && msg.split(SPACE).length == 2) {
            String key = msg.split(SPACE)[0];
            if (key.equals("botId")) {
                sender(entityTypeMessages, "不可配置botId");
                return;
            }
            String value = msg.split(SPACE)[1];
            SetPropertiesSwitch setPropertiesSwitch = new SetPropertiesSwitch();
            if (propSystem.contains(key)) {
                Object oldValue = getPropertiesSwitch.getSystemValue(key, entityTypeMessages);
                setPropertiesSwitch.setSystemColumn(key, value, entityTypeMessages);
                new SelectSystemProperties().flushProperties();
                sender(entityTypeMessages, stringFormatById("已修改设定[%s]\n由<%s>改为<%s>", key, oldValue, value));
            } else if (banSystem.contains(key)) {
                Object oldValue = getPropertiesSwitch.getBanValue(key, entityTypeMessages);
                setPropertiesSwitch.setBanColumn(key, value, entityTypeMessages);
                new SelectBanProperties().flushProperties();
                sender(entityTypeMessages, stringFormatById("已修改设定[%s]\n由<%s>改为<%s>", key, oldValue, value));
            } else if (gameSystem.contains(key)) {
                Object oldValue = getPropertiesSwitch.getGameValue(key, entityTypeMessages);
                setPropertiesSwitch.setGameColumn(key, value, entityTypeMessages);
                new SelectGameProperties().flushProperties();
                sender(entityTypeMessages, stringFormatById("已修改设定[%s]\n由<%s>改为<%s>", key, oldValue, value));
            } else {
                sender(entityTypeMessages, "未找到此关键字");
            }
        } else if (!msg.contains(SPACE)) {
            if (msg.equals("botId")) {
                sender(entityTypeMessages, "不可配置botId");
                return;
            }
            Object result;
            if (propSystem.contains(msg)) {
                result = getPropertiesSwitch.getSystemValue(msg, entityTypeMessages);
            } else if (banSystem.contains(msg)) {
                result = getPropertiesSwitch.getBanValue(msg, entityTypeMessages);
            } else if (gameSystem.contains(msg)) {
                result = getPropertiesSwitch.getGameValue(msg, entityTypeMessages);
            } else {
                sender(entityTypeMessages, "未找到此关键字");
                return;
            }
            sender(entityTypeMessages, String.valueOf(result));
        }

    }
}
